package com.example.administrator.u17.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.u17.R;
import com.example.administrator.u17.modle.SearchTopDetailBean;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by Smile on 2016/11/15.
 */

public class SearchTopDetailAdapter extends BaseAdapter {
    private ArrayList<SearchTopDetailBean.Comics> mData;
    private Context mContext;

    public SearchTopDetailAdapter(Context mContext, ArrayList<SearchTopDetailBean.Comics> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @Override
    public int getCount() {
        return mData==null?0:mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView=View.inflate(mContext,R.layout.search_top_detail_item,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        Picasso.with(mContext).load(mData.get(position).cover).into(holder.ivCover);;
        holder.tvName.setText(mData.get(position).name);
        holder.tvInfo.setText(mData.get(position).description);
        holder.tvCount.setText(mData.get(position).conTag);
        StringBuilder builder=new StringBuilder();
        for (int i = 0; i < mData.get(position).tags.size(); i++) {
            builder.append(mData.get(position).tags.get(i)+" ");
        }
        holder.tvTag.setText(builder);
        int flag=mData.get(position).flag;

        if (flag==1){
            holder.ivType.setBackgroundResource(R.mipmap.icon_comic_pay);
        }else if (flag==3){
            holder.ivType.setBackgroundResource(R.mipmap.icon_comic_vip);
        }else {
            holder.ivType.setVisibility(View.GONE);
        }

        if (position==0){
            holder.ivRank.setBackgroundResource(R.mipmap.icon_ranking_first);
        }else if (position==1){
            holder.ivRank.setBackgroundResource(R.mipmap.icon_ranking_second);
        }else if (position==2){
            holder.ivRank.setBackgroundResource(R.mipmap.icon_ranking_third);
        }else {
            holder.ivRank.setVisibility(View.GONE);
        }

        return convertView;
    }
    class ViewHolder{
        ImageView ivCover,ivType,ivRank;
        TextView tvName,tvTag,tvInfo,tvCount;
        public ViewHolder(View view){
            ivCover= (ImageView) view.findViewById(R.id.iv_cover_search_top_detail_item);
            ivRank= (ImageView) view.findViewById(R.id.iv_rank_search_top_detail_item);
            ivType= (ImageView) view.findViewById(R.id.iv_kind_search_top_detail_item);

            tvCount= (TextView) view.findViewById(R.id.tv_conut_search_top_detail_item);
            tvTag= (TextView) view.findViewById(R.id.tv_flag_search_top_detail_item);
            tvInfo= (TextView) view.findViewById(R.id.tv_info_search_top_detail_item);
            tvName= (TextView) view.findViewById(R.id.tv_name_search_top_detail_item);
        }
    }
}
